﻿using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.Serialization;
using Bookvar.ObjectModel.Collections;

namespace Bookvar.ObjectModel.Actions.Collaboration
{
    [Serializable]
    [DataContract]
    public class AddFloatingTopicsAction : ActionBase
    {
        private TopicCollection newTopics;

        public AddFloatingTopicsAction( IEnumerable<Topic> topics )
        {
            newTopics = new TopicCollection();
            newTopics.AddRange( topics );
        }

        [SuppressMessage( "Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly" )]
        [DataMember]
        public TopicCollection NewTopics
        {
            get { return newTopics; }
            set { newTopics = value; }
        }

        public override void Execute( IExecutionContext context )
        {
            if ( context == null )
            {
                throw new ArgumentNullException( "context" );
            }
            if ( NewTopics == null )
            {
                throw new InvalidOperationException( "NewTopics is not properly set. Value is null." );
            }

            try
            {
                context.ExecutionMode = ExecutionMode.Execution;

                foreach ( Topic topic in NewTopics )
                {
                    context.Mindmap.FloatingTopics.Add( topic );
                    context.HighlightTopic( topic, PublisherGuid );
                }
            }
            finally
            {
                context.ExecutionMode = ExecutionMode.None;
            }
        }
    }
}